
#include "stdafx.h"
#include "HRKExportApp.h"
#include "HRKSceneEnumProc.h"
#include "HRKScene.h"
#include "HRKExport.h"
#include "HRKOptDialog.h"

CHrkExport* CHrkExport::m_pInstance = NULL;

DWORD WINAPI FN_PTHREAD_START_ROUTINE_SCENE(
    LPVOID lpThreadParameter
    )
{
	return 0 ;
}

DWORD WINAPI FN_PTHREAD_START_ROUTINE_ANIM(
    LPVOID lpThreadParameter
    )
{
	return 0 ;
}

CHrkExport::CHrkExport()
{
	CHrkExport::m_pInstance = this;
}

CHrkExport::~CHrkExport() 
{

}

int CHrkExport::ExtCount()
{
	//TODO: Returns the number of file name extensions supported by the plug-in.
	return 1;
}

const TCHAR *CHrkExport::Ext(int n)
{		
	//TODO: Return the 'i-th' file name extension (i.e. "3DS").
	return _T("HRK");
}

const TCHAR *CHrkExport::LongDesc()
{
	//TODO: Return long ASCII description (i.e. "Targa 2.0 Image File")
	return _T("HRK File");
}
	
const TCHAR *CHrkExport::ShortDesc() 
{			
	//TODO: Return short ASCII description (i.e. "Targa")
	return _T("HRK Exporter");
}

const TCHAR *CHrkExport::AuthorName()
{			
	//TODO: Return ASCII Author name
	return _T("*K-Star*");
}

const TCHAR *CHrkExport::CopyrightMessage() 
{	
	// Return ASCII Copyright message
	return _T("Copyright (C) 2007 Minliusoft Corporation");
}

const TCHAR *CHrkExport::OtherMessage1() 
{		
	return _T("");
}

const TCHAR *CHrkExport::OtherMessage2() 
{		
	return _T("");
}

unsigned int CHrkExport::Version()
{				
	return 100;
}

void CHrkExport::ShowAbout(HWND hWnd)
{			
}

BOOL CHrkExport::SupportsOptions(int ext, DWORD options)
{
	return TRUE;
}
int	CHrkExport::DoExport(const TCHAR *filename,ExpInterface *ei,Interface *gi, BOOL suppressPrompts, DWORD options)
{
	_tcscpy_s( m_filename,filename );
	m_ei = ei;
	m_gi = gi;
	m_suppressPrompts = suppressPrompts;
	m_options = options;

	//m_myScene.m_bExportAnim = TRUE;

	m_myScene.m_bHierarky = TRUE;
	m_myScene.m_eCoordSystem = IGameConversionManager::IGAME_D3D;
	m_myScene.m_OutputPrimType = CHrkScene::PT_TRISTRIP;

#ifdef _MFC_BUILD
	_AFX_THREAD_STATE* stat = AfxGetThreadState();
	memcpy(stat,&theApp.m_stat,sizeof(_AFX_THREAD_STATE));

	CWnd*pParent = (CWnd*)CWnd::CreateObject();

	pParent->Attach( m_gi->GetMAXHWnd() );
	CHrkOptDialog opt( pParent);
	
	if( IDCANCEL == opt.DoModal() )
	{
		return TRUE;
	}

	m_myScene.m_bFullSample = opt.m_bFullSample;
	m_myScene.m_bExportAnim = opt.m_bExportAnim;

	if( opt.m_iCoordSel == 0)
	{
		m_myScene.m_eCoordSystem = IGameConversionManager::IGAME_D3D;
	}
	else if( opt.m_iCoordSel == 1)
	{
		m_myScene.m_eCoordSystem = IGameConversionManager::IGAME_OGL;
	}
	else if( opt.m_iCoordSel == 2)
	{
		m_myScene.m_eCoordSystem = IGameConversionManager::IGAME_MAX;
	}

	if(opt.m_bBuildStrips)
	{
		m_myScene.m_OutputPrimType = CHrkScene::PT_TRISTRIP;
	}
	else
	{
		m_myScene.m_OutputPrimType = CHrkScene::PT_TRILIST;
	}
	
	pParent->Detach();
	delete pParent;

#endif

	bool bExportSelected = (options & SCENE_EXPORT_SELECTED) ? true : false;

	m_os.open( m_filename );
	
	if(!m_os.is_open())
	{
		theApp.MessageBoxV( _T("Error"),_T( "Can't open file:%s"),filename);
		
		return FALSE;
	}

	GetCOREInterface()->ProgressStart( _T("Export HRK Scene..."),TRUE,FN_PTHREAD_START_ROUTINE_SCENE,NULL);

	INode * pRootNode = GetCOREInterface()->GetRootNode();

	m_myScene.ExportGameScene( m_os, bExportSelected, pRootNode);

	m_os.close();

	GetCOREInterface()->ProgressEnd();
	
	if(m_myScene.m_bExportAnim)
	{
		if(m_os.is_open()){
			m_os.close();
		}

		TCHAR filename_anim[MAX_PATH];

		_tcscpy_s(filename_anim,m_filename);
		char* s = strrchr(filename_anim,'.');
		if(s){
			_tcscpy_s(s,MAX_PATH - strlen(m_filename) + strlen(s),_T(".anm"));
		}
		else{
			_tcscat_s(filename_anim,MAX_PATH - strlen(m_filename),_T(".anm"));
		}

		m_os.open( filename_anim );
		GetCOREInterface()->ProgressStart( _T("Export HRK Animation..."),TRUE,FN_PTHREAD_START_ROUTINE_ANIM,NULL);
		
		m_myScene.ExportGameAnim( m_os,bExportSelected );

		m_os.close();

		GetCOREInterface()->ProgressEnd();
	}

	theApp.MessageBoxV( _T("Success"),_T( "File export success: %s"),filename);


	return TRUE;
}

BOOL CHrkExport::TestIfSplit(INode * pNode)
{
	if(pNode->NumberOfChildren())
		return TRUE;
	else
		return FALSE;
}